from django.shortcuts import redirect
from django.utils.deprecation import MiddlewareMixin


class AuthMiddleware(MiddlewareMixin):
    """中间件"""

    def process_request(self, request):
        # 排除无需登录就能访问的页面
        if request.path_info == "/index/" or request.path_info == "/index/user/login/" or request.path_info == "/index/manager/login/" or request.path_info == "/index/user/login/user/register/" or request.path_info == "/index/manager/register/":
            return
        info_dict = request.session.get('info')
        # print(info_dict)
        if info_dict and info_dict["type"] == 'user' and request.path_info.startswith("/user/"):
            return
        if info_dict and info_dict["type"] == 'manager' and request.path_info.startswith("/manager/"):
            return
        return redirect('/index/')
